package malus.shaofeng.com.beiwomusic.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import malus.shaofeng.com.beiwomusic.R;

/**
 * Created by aaa on 15-3-12.
 */
public class ScrollChangeScrollView extends ScrollView {

    private LinearLayout layout;
    private TextView title;
    private ImageView image;
    private LinearLayout bottom;
    private Context context;

    public ScrollChangeScrollView(Context context) {
        this(context, null);
    }

    public ScrollChangeScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScrollChangeScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
    }

    public void registerView(LinearLayout layout) {
        this.layout = layout;
    }

    public void registerTitle(TextView title) {
        this.title = title;
    }

    public void registerBar(ImageView image) {
        this.image = image;
    }

    public void registerBottom(LinearLayout bottom){
        this.bottom = bottom;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (0 <= t && t <= 255) {
            String s = Integer.toHexString(t);
            if (t <= 15) {
                s = "0" + s;
            }
            String color = "#" + s + "ffffff";
            layout.setBackgroundColor(Color.parseColor(color));
            int f = 255 - t;
            if (0 <= f && f <= 255) {
                s = Integer.toHexString(f);
                if (f <= 15) {
                    s = "0" + s;
                }
                color = "#ff" + s + s + s;
                title.setTextColor(Color.parseColor(color));
            }
        }
        if (t >= 200) {
            image.setImageResource(R.drawable.article_acionbar_play_press);
        } else {
            image.setImageResource(R.drawable.article_acionbar_play_press_top);
        }

        if (oldt>t){
            Animation animation = AnimationUtils.loadAnimation(context, R.anim.bottom_trans_out);
            if (bottom.getVisibility()==GONE){
                bottom.setVisibility(VISIBLE);
                bottom.startAnimation(animation);
             }
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    bottom.setVisibility(VISIBLE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }else if (oldl<t){
            Animation animation = AnimationUtils.loadAnimation(context, R.anim.bottom_trans_in);
            if (bottom.getVisibility()==VISIBLE)
                bottom.startAnimation(animation);
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    bottom.setVisibility(GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }

    }
}
